/**
 * @author LKQ
 * @date 2022/2/21 16:40
 * @description
 */
public class FormalSolution {
    public static void main(String[] args) {
        FormalSolution solution = new FormalSolution();
        solution.customSortString("cbafg", "abcd");
    }
    public String customSortString(String order, String s) {
        int[] count = new int[26];
        for (char c: s.toCharArray()) {
            count[c - 'a']++;
        }
        StringBuffer ans = new StringBuffer();
        for (char c: order.toCharArray()) {
            // 按照order字符顺序，加入s中存在同一字符
            for (int i = 0; i < count[c - 'a']; i++) {
                ans.append(c);
            }
            count[c-'a'] = 0;
        }
        // 加入剩余的字符
        for (char c ='a'; c <= 'z'; c++) {
            for (int i = 0; i < count[c-'a']; ++i) {
                ans.append(c);
            }
        }
        return ans.toString();
    }
}
